﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;

/// <summary>
/// Summary description for CIndex
/// </summary>
public class CIndex
{
	public CIndex()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public DataRow Read(int title)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        DataTable dt = d.ExeDataTable("select * from [index] where title=@Title", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }

    public DataTable ReadAll()
    {
        DataAccess d = new DataAccess();
        d.Open();
        DataTable dt = d.ExeDataTable("select * from [index] order by title", null);
        d.Close();
        return dt;
    }

    public bool UpdatePath(int title, string path,System.Web.UI.Page p)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        DataTable dt = d.ExeDataTable("select * from [index] where title=@Title", pars);
        if (null != dt && dt.Rows[0]["path"].ToString() != "")
        {
            try
            {
                if (System.IO.File.Exists(p.Server.MapPath("~/" + dt.Rows[0]["path"].ToString())))
                {
                    System.IO.File.Delete(p.Server.MapPath("~/" + dt.Rows[0]["path"].ToString()));
                }
            }
            catch (Exception)
            { }
        }
        pars.Clear();

        pars.Add("Path", path);
        pars.Add("Title", title);
        int c = d.ExeNonQuery("update [index] set [path]=@Path where title=@Title", pars);
        d.Close();
        if (c>0)
        {
            return true;
        }
        return false;
    }

    public bool UpdateProduct(int title, int pid)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Product", pid);
        pars.Add("Title", title);
        int c = d.ExeNonQuery("update [index] set product=@Product where title=@Title", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }
}